
module SinatraHelpers
  class SinatraApp
    def self.invoke
      app = new
      app.create_webapp_rb
      app.create_config_ru
      app.create_index_haml
      app.create_layout_haml
    end
    def create_webapp_rb
      create_file_with_content("./webapp.rb", webapp_rb)
    end
    def create_config_ru
      create_file_with_content( "./config.ru", config_ru )
    end
    def create_index_haml content = index_haml
      create_file_with_content( "./views/index.haml", content )
    end
    def create_layout_haml content = layout_haml
      create_file_with_content( "./views/layout.haml", content )
    end

    private
    def webapp_rb
      "get '/' do\n  haml :index\nend"
    end
    def config_ru
      "require 'bundler/setup'\n" \
        "require 'sinatra'\n" \
        "require 'haml'\n\n" \
        "require './webapp.rb'\n\n" \
        "run Sinatra::Application"
    end
    def index_haml
      "%h1 Hello\n"
    end
    def layout_haml
      "!!! 5\n" \
        "%html\n" \
        "  %head\n" \
        "    %meta{ :'http-equiv' => 'Content-Type', :content => 'text/html; charset=utf-8' }\n" \
        "  %body\n" \
        "    = yield\n"
    end
    def create_file_with_content file_path, content
      File.open(file_path, "w") do |f|
        f.write( content )
      end
    end
  end
end
